--- title: "06-bot界面 增删改查" created: 2025-12-02 tags: - 项目 aliases: - bot界面 增删改查 --- # bot界面 增删改查 ## 需求分析和准备工作 实现这么一个页面 ![[a71ee7973dfc6523fcca6aeb0d33c9c5-7dce4a18.png]] 主要是对bot的一个增删改查操作 前提就是创一个数据表来存储它 ### 实体类 在数据库中创建表bot 表中包含的列: id: int:非空、自动增加、唯一、主键 user\_id: int:非空 注意:在pojo中需要定义成userId,在queryWrapper中的名称仍然为user\_id title: varchar(100) description: varchar(300) content:varchar(10000) rating: int:默认值为1500 createtime: datetime pojo中定义日期格式的注解:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") modifytime: datetime pojo中定义日期格式的注解:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") ```sql CREATE TABLE `kob`.`bot` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NOT NULL, `title` varchar(100) NULL, `description` varchar(300) NULL, `content` varchar(10000) NULL, `rating` int NULL DEFAULT 1500, `createtime` datetime NULL, `modifytime` datetime NULL, PRIMARY KEY (`id`) ); ``` ![[image-3ed75f77.png]] pojo中 ```java package com.zwnsyw.backend.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @NoArgsConstructor @AllArgsConstructor public class Bot { @TableId(type = IdType.AUTO) private Integer id; private Integer userId;//注意 数据库中下划线命名对应pojo驼峰命名 private String title; private String description; private String content; private Integer rating; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss") private Date createtime; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss") private Date modifytime; } ``` mapper ```java package com.zwnsyw.backend.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.zwnsyw.backend.pojo.Bot; import org.apache.ibatis.annotations.Mapper; @Mapper public interface BotMapper extends BaseMapper { } ``` ## 增删改查api 已经熟悉了 三部曲 service、serviceimpl、controller 先写接口 再写实现 再写调用 #### Service ![[image-457bbc8b.png]] ##### AddService ```java package com.zwnsyw.backend.service.bot; import java.util.Map; public interface AddService { public Map add(Map data); } ``` ##### RemoveService ```java package com.zwnsyw.backend.service.bot; import java.util.Map; public interface RemoveService { Map remove(Map data); } ``` ##### UpdateService ```java package com.zwnsyw.backend.service.bot; import java.util.Map; public interface UpdateService { Map update(Map data); } ``` ##### GetListService ```java package com.zwnsyw.backend.service.bot; import com.zwnsyw.backend.pojo.Bot; import java.util.List; public interface GetListService { List getList(); } ``` #### ServiceImpl 三部曲 @Service 、 Implements ?Service、 alt+insert实现方法 ##### AddServiceImpl ```java package com.zwnsyw.backend.service.impl.bot; import com.zwnsyw.backend.mapper.BotMapper; import com.zwnsyw.backend.pojo.Bot; import com.zwnsyw.backend.pojo.User; import com.zwnsyw.backend.service.bot.AddService; import com.zwnsyw.backend.service.impl.utils.UserDetailsImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service public class AddServiceImpl implements AddService { @Autowired private BotMapper botMapper; @Override public Map add(Map data) { //要知道是哪个用户在操作 需要先取出用户信息 从token中得到 所以比较麻烦 UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal(); User user = loginUser.getUser(); //需要拿到哪些数据 看表来 //id自增不用管 user_id刚取得了 //title需要传来 描述需要传来 内容需要传来 //分数默认1500不用管 创建时间是现在不用管 修改时间默认是现在不用管 String title = data.get("title"); String description = data.get("description"); String content = data.get("content"); Map map = new HashMap<>(); //加一系列判断 if(title == null || title.length() == 0){ map.put("error_message","标题不能为空"); return map; } if(title.length() > 100){ map.put("error_message","标题长度不能大于100"); return map; } //描述可以为空 if(description == null || description.length()==0){ description="这个用户很懒,什么也没留下~"; } if(description.length()>300){ map.put("error_message","Bot的描述不能超过300"); return map; } if(content == null || content.length() == 0){ map.put("error_message","代码不能为空"); return map; } if(content.length()>10000){ map.put("error_message","代码长度不能超过10000"); return map; } Date now=new Date(); Bot bot = new Bot(null,user.getId(),title,description,content,1500,now,now); //添加到数据库中 需要注入接口 BotMapper botMapper.insert(bot); map.put("error_message","success"); return map; } } ``` ##### RemoveServiceImpl ```java package com.zwnsyw.backend.service.impl.bot; import com.zwnsyw.backend.mapper.BotMapper; import com.zwnsyw.backend.pojo.Bot; import com.zwnsyw.backend.pojo.User; import com.zwnsyw.backend.service.bot.RemoveService; import com.zwnsyw.backend.service.impl.utils.UserDetailsImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; @Service public class RemoveServiceImpl implements RemoveService { @Autowired private BotMapper botMapper; @Override public Map remove(Map data) { //取出当前用户 用于鉴权 是否有权限删除该bot UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal(); User user = loginUser.getUser(); int botId = Integer.parseInt(data.get("botId")); Bot bot = botMapper.selectById(botId); Map map = new HashMap<>(); if(bot == null){ map.put("error_message","Bot不存在或已被删除"); return map; } if(!bot.getUserId().equals(user.getId())){ map.put("error_message","没有权限删除Bot"); return map; } botMapper.deleteById(botId); map.put("error_message","success"); return map; } } ``` ##### UpdateServiceImpl ```java package com.zwnsyw.backend.service.impl.bot; import com.zwnsyw.backend.mapper.BotMapper; import com.zwnsyw.backend.pojo.Bot; import com.zwnsyw.backend.pojo.User; import com.zwnsyw.backend.service.bot.UpdateService; import com.zwnsyw.backend.service.impl.utils.UserDetailsImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service public class UpdateServiceImpl implements UpdateService { @Autowired private BotMapper botMapper; @Override public Map update(Map data) { UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal(); User user = loginUser.getUser(); //更新谁 —— botId //更新哪些数据 user_id不会变 title会变 描述和内容会变 分数不能变 创建时间不能变 修改时间自动变 int botId = Integer.parseInt(data.get("botId")); String title = data.get("title"); String description = data.get("description"); String content = data.get("content"); Map map = new HashMap<>(); //加一系列判断 if(title == null || title.length() == 0){ map.put("error_message","标题不能为空"); return map; } if(title.length() > 100){ map.put("error_message","标题长度不能大于100"); return map; } //描述可以为空 if(description == null || description.length()==0){ description="这个用户很懒,什么也没留下~"; } if(description.length()>300){ map.put("error_message","Bot的描述不能超过300"); return map; } if(content == null || content.length() == 0){ map.put("error_message","代码不能为空"); return map; } if(content.length()>10000){ map.put("error_message","代码长度不能超过10000"); return map; } Bot bot = botMapper.selectById(botId); if(bot == null){ map.put("error_message","Bot不存在或已被删除"); return map; } if(!bot.getUserId().equals(user.getId())){ map.put("error_message","没权限修改该Bot"); return map; } Bot newbot = new Bot( bot.getId(), user.getId(), title, description, content, bot.getRating(), bot.getCreatetime(), new Date() ); botMapper.updateById(newbot); map.put("error_message","success"); return map; } } ``` ##### GetListServiceImpl ```java package com.zwnsyw.backend.service.impl.bot; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.zwnsyw.backend.mapper.BotMapper; import com.zwnsyw.backend.pojo.Bot; import com.zwnsyw.backend.pojo.User; import com.zwnsyw.backend.service.bot.GetListService; import com.zwnsyw.backend.service.impl.utils.UserDetailsImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.List; @Service public class GetListServiceImpl implements GetListService { @Autowired private BotMapper botMapper; @Override public List getList() { UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal(); User user = loginUser.getUser(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("user_id", user.getId()); return botMapper.selectList(queryWrapper); } } ``` #### controller 三部曲 @RestController 注入接口@Autowired ?Service get/postMapping(接口地址) @RequestParam 绑定数据 ##### AddController ```java package com.zwnsyw.backend.controller.user.bot; import com.zwnsyw.backend.service.bot.AddService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController public class AddController { @Autowired private AddService addService; @PostMapping("/user/bot/add/") public Map add(@RequestParam Map data){ return addService.add(data); } } ``` ###### test ```javascript ``` ![[image-823326d3.png]] ![[image-6890445f.png]] ##### RemoveController ```java package com.zwnsyw.backend.controller.user.bot; import com.zwnsyw.backend.service.bot.RemoveService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController public class RemoveController { @Autowired private RemoveService removeService; @PostMapping("/user/bot/remove/") public Map remove(@RequestParam Map data){ return removeService.remove(data); } } ``` ###### test ```javascript $.ajax({ url: "http://localhost:3000/user/bot/remove/", type: "post", data:{ botId:5, }, headers:{ Authorization:"Bearer " + store.state.user.token, }, success(resp){ console.log(resp); }, error(resp){ console.log(resp); } }) ``` ![[image-d1e27534.png]] ![[image-2edfb08a.png]] ![[image-065b83e3.png]] ##### UpdateController ```java package com.zwnsyw.backend.controller.user.bot; import com.zwnsyw.backend.service.bot.UpdateService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController public class UpdateController { @Autowired private UpdateService updateService; @PostMapping("/user/bot/update/") public Map update(@RequestParam Map data) { return updateService.update(data); } } ``` ###### test ```javascript $.ajax({ url: "http://localhost:3000/user/bot/update/", type: "post", data:{ botId : 1, title:"修改Bot的标题", description:"修改Bot的描述", content:"修改Bot的代码", }, headers:{ Authorization:"Bearer " + store.state.user.token, }, success(resp){ console.log(resp); }, error(resp){ console.log(resp); } }) ``` ![[image-e38e8515.png]] ![[image-6712a2be.png]] ##### GetListController ```java package com.zwnsyw.backend.controller.user.bot; import com.zwnsyw.backend.pojo.Bot; import com.zwnsyw.backend.service.bot.GetListService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class GetListController { @Autowired private GetListService getListService; @GetMapping("/user/bot/getlist/") public List getList(){ return getListService.getList(); } } ``` ###### test ```javascript $.ajax({ url: "http://localhost:3000/user/bot/getlist/", type: "get", headers:{ Authorization:"Bearer " + store.state.user.token, }, success(resp){ console.log(resp); }, error(resp){ console.log(resp); } }) ``` ![[image-bc8625a8.png]] ## 前端部分 大概布局 ![[a71ee7973dfc6523fcca6aeb0d33c9c5-7dce4a18.png]] ```html ``` 后端表结构有修改 对应接口需要改变 --- **项目分区导航**: [[03-WebSocket实战篇|WebSocket实战篇]] ⬅️ | 06-bot界面 增删改查 | ➡️ [[07-bot代码执行 排行榜页面|bot代码执行 排行榜页面]]